package com.yuandian.management.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import com.google.common.base.Splitter;
import com.yuandian.api.management.entity.*;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.admin.api.feign.RemoteDictService;
import com.yuandian.management.enums.IndicatorType;
import com.yuandian.management.mapper.FlowConfMapper;
import com.yuandian.management.service.FlowConfService;
import com.yuandian.management.utils.IpUtil;
import com.yuandian.management.vo.ModuleBaseConfigVo;
import com.yuandian.management.vo.flow.OriginalDataFilterEntity;
import com.yuandian.management.vo.ModuleNetworkAgentVo;
import com.yuandian.management.vo.flowConf.*;
import com.yuandian.management.vo.flowConf.enums.FlowDataTypeEnum;
import com.yuandian.management.vo.flowConf.enums.StepType;
import com.yuandian.management.vo.flowConf.pojo.*;
import com.yuandian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 描述: 实时流读取配置表接口-实现
 *
 * @author lxixi
 * @create 2023-06-02 14:55
 */
@Service
public class FlowConfServiceImpl implements FlowConfService {
	@Autowired
	private FlowConfMapper flowConfMapper;

	@Autowired
	private RemoteDictService remoteDictService;

	@Override
	public AreaConf getAreaConf() {
		HashMap<String, String> areaNameMap = new HashMap<>();
		HashMap<String, String> areaRegionMap = new HashMap<>();

		List<ConfArea> list = flowConfMapper.getAreaInfo();
		for (ConfArea c : list) {
			areaNameMap.put(c.getCode(), c.getName());
			areaRegionMap.put(c.getCode(), String.valueOf(c.getId()));
		}
		areaNameMap.put("000000", "未知");

		AreaInfo areaInfo = new AreaInfo();
		areaInfo.setAreaNameMap(areaNameMap);
		areaInfo.setAreaRegionMap(areaRegionMap);

		AreaConf areaConf = new AreaConf();
		areaConf.setAreaInfo(areaInfo);
		return areaConf;
	}

	@Override
	public BusinessHallConf getBusinessHallConf() {
		HashMap<String, String> map = new HashMap<>();
		List<Map<String, String>> list = flowConfMapper.getBusinessHallAreaMap();
		list.forEach(e -> map.put(e.get("num"), e.get("area_code")));

		BusinessHallConf businessHallConf = new BusinessHallConf();
		businessHallConf.setBusinessHallAreaMap(map);
		return businessHallConf;
	}

	@Override
	public ChannelMappingConf getChannelMappingConf() {
		HashMap<String, String> channelMappingMap = new HashMap<>();
		Map<String, List<Integer>> channelSysRelationMap = new HashMap<>();

		List<ConfChannelMapping> list = flowConfMapper.getChannelMappingInfo();
		for (ConfChannelMapping channelMapping : list) {
			channelMappingMap.put(channelMapping.getChannelCode(), channelMapping.getChannelName());

			List<Integer> sysIds = channelSysRelationMap.computeIfAbsent(channelMapping.getChannelCode(), k -> new ArrayList<>());
			sysIds.add(channelMapping.getSystemId());
		}

		ChannelInfo channelInfo = new ChannelInfo();
		channelInfo.setChannelMappingMap(channelMappingMap);
		channelInfo.setChannelSysRelationMap(channelSysRelationMap);

		ChannelMappingConf channelMappingConf = new ChannelMappingConf();
		channelMappingConf.setChannelInfo(channelInfo);
		return channelMappingConf;
	}

	@Override
	public ConfirmedResourceConf getConfirmedResourceConf() {
		HashMap<String, Integer> resourceMap = new HashMap<>();
		List<ConfResource> list = flowConfMapper.getConfirmedResource();
		for (ConfResource entity : list) {
			String key = entity.getResourceCode();
			Integer identifyType = entity.getIdentifyType();
			if (identifyType == 1) {
				key += "-" + FlowDataTypeEnum.BPM_WEB.getDataType() +
						"-" + entity.getType();
			} else if (identifyType == 2) {
				key += "-" + FlowDataTypeEnum.BPM_MID.getDataType();
			} else if (identifyType == 3) {
				key += "-" + FlowDataTypeEnum.BPM_DB.getDataType();
			}
			resourceMap.put(key, 1);
		}

		ConfirmedResourceConf confirmedResourceConf = new ConfirmedResourceConf();
		confirmedResourceConf.setConfirmedResourceMap(resourceMap);
		return confirmedResourceConf;
	}

	@Override
	public IpSegmentComRelationConf getIpSegmentComRelationConf(List<String> segmentTypes) {
		List<ConfChannelMapping> confChannelMappings = flowConfMapper.getChannelMappingInfo();

		HashMap<Integer, List<String>> systemAndChannelList = new HashMap<>();
		for (ConfChannelMapping cm : confChannelMappings) {
			List<String> channelCodes = systemAndChannelList.computeIfAbsent(cm.getSystemId(), k -> new ArrayList<>());
			channelCodes.add(cm.getChannelCode());
		}

		HashMap<String, List<SegmentComponentRelation>> map = new HashMap<>();
		List<SegmentComponentRelation> segmentComponentRelations = flowConfMapper.getSegmentComponentRelation(segmentTypes);
		for (SegmentComponentRelation scr : segmentComponentRelations) {
			scr.setChannelList(systemAndChannelList.get(scr.getSystemId()));
			scr.setIp(IpUtil.customIpStr2number(scr.getIpStr()));
			List<SegmentComponentRelation> list = map.computeIfAbsent(scr.getIp(), k -> new ArrayList<>());
			if (list.stream().noneMatch(
					item -> item.getIp().equals(scr.getIp()) && item.getPort().equals(scr.getPort()))) {
				list.add(scr);
			}
		}

		IpSegmentComRelationConf ipSegmentComRelationConf = new IpSegmentComRelationConf();
		ipSegmentComRelationConf.setIpSegComRelationshipConfig(map);
		return ipSegmentComRelationConf;
	}

	@Override
	public DetailConf getDetailConf() {
//		Map<Integer, List<StepJoinMapping>> sjmMap = getSjmMap();
		Map<String, List<String>> exclusiveMap = new HashMap<>();
		Map<String, Map<String, List<String>>> exclusiveExtractConf = new HashMap<>();
		List<ExclusiveIndicator> exclusiveIndicatorList = flowConfMapper.queryExclusiveIndicatorList();
		for (ExclusiveIndicator entity : exclusiveIndicatorList) {
			List<String> exclusiveList = exclusiveMap.computeIfAbsent(entity.getResourceCode(), k -> new ArrayList<>());
			exclusiveList.add(entity.getName());

			String effect = entity.getEffect();
			if (StringUtils.isEmpty(effect)) {
				continue;
			}
			String[] effectStrArr = effect.split("\\|");
			for (String effectStr : effectStrArr) {
				// 加载专属指标配置-详单组合
				IndicatorType[] indicatorTypes = {
						IndicatorType.deal, IndicatorType.businessHall, IndicatorType.phone,
						IndicatorType.operator, IndicatorType.cusResCode, IndicatorType.backfill};
				for (IndicatorType indicatorType : indicatorTypes) {
					if ((indicatorType.getKey() + ":yes").equals(effectStr)) {
						Map<String, List<String>> exclusiveExtractMap = exclusiveExtractConf.computeIfAbsent(indicatorType.getKey(), k -> new HashMap<>());
						List<String> exclusiveExtractList = exclusiveExtractMap.computeIfAbsent(entity.getResourceCode(), k -> new ArrayList<>());
						if (!exclusiveExtractList.contains(entity.getName())) {
							exclusiveExtractList.add(entity.getName());
						}
						break;
					}
				}
			}
		}

		Map<String, List<StepConfigurationInfo>> stepConfigurationMap = new HashMap<>();
		Map<String, List<StepConfigurationInfo>> businessConfs = new HashMap<>();
		Map<String, Integer> stepCodeAndSequenceMap = new HashMap<>();

		List<SysBusiStepRelation> list = flowConfMapper.querySysBusiStepConfigList();
		for (SysBusiStepRelation sbs : list) {
			Integer systemId = sbs.getSystemId();
			String resourceCode = sbs.getResourceCode();
			String businessCode = sbs.getBusinessCode();
//			if (sbs.getIsConfirm() == 1) {
//				this.sysBusiProcessConfigMap.put(systemId + S.VERTICAL_BAR.s() + resourceCode, sbs);
//			}
			// 如果是web，还需加载业务步骤配置-详单组合
			String stepSystemKey = resourceCode + "@" + systemId;
			String systemBusinessKey = systemId + "@" + businessCode;
			List<StepConfigurationInfo> stepConfList = stepConfigurationMap.computeIfAbsent(stepSystemKey, k -> new ArrayList<>());
			List<StepConfigurationInfo> businessConfList = businessConfs.computeIfAbsent(systemBusinessKey, k -> new ArrayList<>());
			StepConfigurationInfo stepConf = new StepConfigurationInfo();
			// 系统id
			stepConf.setSystemId(systemId);
			Map<String, List<String>> indicatorMap = exclusiveExtractConf.get(IndicatorType.deal.getKey());
			if (!CollectionUtils.isEmpty(indicatorMap)) {
				List<String> eiList = indicatorMap.get(resourceCode);
				if (!CollectionUtils.isEmpty(eiList)) {
					// 业务交易识别字段名称
					stepConf.setDealIdentifyField(eiList.get(0));
				}
			}
			// 步骤类型
			if (sbs.getStepType() == 1) {
				stepConf.setStepType(StepType.first);
			} else if (sbs.getStepType() == 3) {
				stepConf.setStepType(StepType.last);
			} else {
				stepConf.setStepType(StepType.mid);
			}
			// 是否单步骤业务
			stepConf.setSingleStep(1 == sbs.getIsSingleStep());
			// 业务编码
			stepConf.setBusinessCode(businessCode);
			// 业务名称
			stepConf.setBusinessName(sbs.getBusinessName());
			// 步骤编码
			stepConf.setStepCode(resourceCode);
			// 步骤名称
			stepConf.setStepName(sbs.getStepName());
			// 步骤顺序
			stepConf.setStepSequence(sbs.getSequence());
			// 是否配置专属指标映射
			stepConf.setConfigureStrategy(sbs.getIsConfigureStrategy() == 1);
			// 如果配置了专属指标映射，则设置映射字段对象
			if (stepConf.isConfigureStrategy()) {
				List<String> eis = exclusiveMap.get(sbs.getResourceCode());
				if (!CollectionUtils.isEmpty(eis)) {
//					StepJoinMappingInfo sjm = new StepJoinMappingInfo();
//					sjm.setMatchNum(1);
//					sjm.setMappingType(MappingType.unsequence);
//					sjm.setBusinessCode(stepConf.getBusinessCode());
//					sjm.setStepCode(stepConf.getStepCode());
//					List<String> exclusiveList = sjms.stream().map(StepJoinMapping::getStepExclusiveIndex).collect(Collectors.toList());
//					sjm.setStepExclusieveList(exclusiveList);
					stepConf.setExclusiveList(eis);
				}
			}
			// 优先级，处理首步骤共用的情况
			if (sbs.getConfirmTime() != null) {
				stepConf.setPriority(DateUtil.format(sbs.getConfirmTime(), "yyyy-MM-dd HH:mm:ss"));
			} else {
				String INFINITE = "2099-01-01 12:00:00";
				stepConf.setPriority(INFINITE);
			}
			// 是否允许失败 - 用于业务合规性判断
			stepConf.setIsAllowfail(sbs.getIsAllowFail());
			// 是否允许确实 - 用于业务合规性判断
			stepConf.setIsAllowLose(sbs.getIsAllowLose());

			stepConfList.add(stepConf);
			businessConfList.add(stepConf);

			// 步骤顺序
			if (stepCodeAndSequenceMap.containsKey(resourceCode)) {
				stepCodeAndSequenceMap.put(resourceCode, Math.min(stepCodeAndSequenceMap.get(resourceCode), sbs.getSequence()));
			} else {
				stepCodeAndSequenceMap.put(resourceCode, sbs.getSequence());
			}
		}

		// 排序
		for (String key : stepConfigurationMap.keySet()) {
			List<StepConfigurationInfo> sciList = stepConfigurationMap.get(key);
			sciList.sort(Comparator.comparing(info -> {
				// 共用步骤
				if (sciList.size() > 1) {
					info.setShare(true);
				}
				return info.getPriority();
			}));
		}

		// step_1：缓存 [步骤编码 -> 并查集节点下标] 的关系
		Map<String, Integer> stepCodeAndIndex = new HashMap<>();
		int index = 0;
		for (List<StepConfigurationInfo> infoList : stepConfigurationMap.values()) {
			for (StepConfigurationInfo info : infoList) {
				if (!stepCodeAndIndex.containsKey(info.getStepCode())) {
					stepCodeAndIndex.put(info.getStepCode(), index++);
				}
			}
		}

		// step_2：通过并查集，合并业务下有交集的步骤
		DisjointSetUnion disjointSetUnion = new DisjointSetUnion(stepCodeAndIndex.size());
		for (List<StepConfigurationInfo> infoList : stepConfigurationMap.values()) {
			if (infoList.size() > 1) {
				StepConfigurationInfo first = infoList.get(0);
				for (int i = 1; i < infoList.size(); i++) {
					StepConfigurationInfo next = infoList.get(i);
					disjointSetUnion.union(stepCodeAndIndex.get(first.getStepCode()), stepCodeAndIndex.get(next.getStepCode()));
				}
			}
		}


		DetailRelationInfo detailRelationInfo = new DetailRelationInfo();

		detailRelationInfo.setStepConfigurationMap(stepConfigurationMap);
		detailRelationInfo.setBusinessConfs(businessConfs);
		detailRelationInfo.setExclusiveExtractConf(exclusiveExtractConf);
//		detailRelationInfo.setStepCodeAndSequenceMap(stepCodeAndSequenceMap);
		detailRelationInfo.setStepCodeAndIndex(stepCodeAndIndex);
		detailRelationInfo.setDisjointSetUnion(disjointSetUnion);

		DetailConf detailConf = new DetailConf();
		detailConf.setDetailRelationInfo(detailRelationInfo);
		return detailConf;
	}

	@Override
	public EmployeeConf getEmployeeConf() {
		Map<String, EmployeeEntity> employeeConfig = new HashMap<>();

		List<EmployeeEntity> list = flowConfMapper.queryEmployeeList();
		for (EmployeeEntity e : list) {
			EmployeeEntity employeeEntity = employeeConfig.get(e.getNum());
			if (employeeEntity == null) {
				e.setBusinessHallList(new ArrayList<>(Collections.singletonList(e.getBusinessHallNum())));
				employeeConfig.put(e.getNum(), e);
			} else {
				employeeEntity.setId(e.getId());
				employeeEntity.setBusinessHallNum(e.getBusinessHallNum());
				employeeEntity.setName(e.getName());
				employeeEntity.setIsCheckInactive(e.getIsCheckInactive());
				employeeEntity.setAreaCode(e.getAreaCode());
				employeeEntity.getBusinessHallList().add(e.getAreaCode());
			}
		}

		EmployeeConf employeeConf = new EmployeeConf();
		employeeConf.setEmployeeConfig(employeeConfig);
		return employeeConf;
	}

	@Override
	public ExclusiveIndicatorConf getExclusiveIndicatorConf() {
		Map<String, String> channelIndicatorMap = new HashMap<>();
		Map<String, Map<String, String>> exclusiveIndicatorConfig = new HashMap<>(65536);
		List<ExclusiveIndicator> exclusiveIndicatorList = flowConfMapper.queryExclusiveIndicatorList();
		for (ExclusiveIndicator entity : exclusiveIndicatorList) {
			String effect = entity.getEffect();
			if (StringUtils.isEmpty(effect)) {
				continue;
			}
			Map<String, String> effectMap = exclusiveIndicatorConfig.computeIfAbsent(entity.getResourceCode() + "_" + entity.getEiCode(), f -> new HashMap<>());
			String[] effectStrArr = effect.split("\\|");
			for (String effectStr : effectStrArr) {
				String[] effectArr = effectStr.split(":");
				if (effectArr.length >= 2) {
					effectMap.put(effectArr[0], effectArr[1]);
				}
				if (effectStr.contains("channel:yes")) {
					channelIndicatorMap.put(entity.getResourceCode(), entity.getName());
				}
			}
		}

		ExclusiveIndicatorInfo eciInfo = new ExclusiveIndicatorInfo();
		eciInfo.setExclusiveIndicatorConfig(exclusiveIndicatorConfig);
		eciInfo.setChannelIndicatorMap(channelIndicatorMap);

		ExclusiveIndicatorConf exclusiveIndicatorConf = new ExclusiveIndicatorConf();
		exclusiveIndicatorConf.setExclusiveIndicatorInfo(eciInfo);
		return exclusiveIndicatorConf;
	}

	@Override
	public ModuleConf getModuleConf() {
		List<ModuleBaseConfigVo> list = flowConfMapper.queryModuleConf();
		Map<String, String> map = new HashMap<>();
		list.forEach(m -> map.put(m.getConfigName(), m.getConfigDetails()));

		ModuleConf moduleConf = new ModuleConf();
		moduleConf.setModuleConf(map);
		return moduleConf;
	}

	@Override
	public SessionIdFillBackConf getSessionIdFillBackConf() {
		Map<String, Map<String, String>> sessionIdFillBackConfig = new HashMap<>();
		Map<String, String> getMap = sessionIdFillBackConfig.computeIfAbsent("GET", f -> new HashMap<>());
		Map<String, String> saveMap = sessionIdFillBackConfig.computeIfAbsent("SAVE", f -> new HashMap<>());
		List<SessionIdFillBack> list = flowConfMapper.querySessionIdFillBackList();
		for (SessionIdFillBack s : list) {
			if (BooleanUtil.isTrue(s.isJoinOnGetSessionIdIndicator())) {
				if (!StringUtils.isEmpty(s.getGetSessionIdIndicator())) {
					getMap.put(s.getStepCode(), s.getEiCode());
				}
			} else {
				if (!StringUtils.isEmpty(s.getSaveSessionIdIndicator())) {
					saveMap.put(s.getStepCode(), s.getEiCode());
				}
			}
		}

		SessionIdFillBackConf sessionIdFillBackConf = new SessionIdFillBackConf();
		sessionIdFillBackConf.setSessionIdFillBackConfig(sessionIdFillBackConfig);
		return sessionIdFillBackConf;
	}

	@Override
	public StepSequenceConf getStepSequenceConf() {
		HashMap<String, Integer> map = new HashMap<>();
		List<ConfStep> list = flowConfMapper.queryStepSequence();
		list.forEach(e -> map.put(e.getStepCode(), e.getSequence()));

		StepSequenceConf stepSequenceConf = new StepSequenceConf();
		stepSequenceConf.setStepCodeAndSequenceMap(map);
		return stepSequenceConf;
	}

	@Override
	public SystemBusiStepRelationConf getSystemBusiStepRelationConf() {
		Map<String, SysBusiStepRelation> map = new HashMap<>();
		List<SysBusiStepRelation> list = flowConfMapper.querySystemBusiStepRelationList();
		for (SysBusiStepRelation s : list) {
			map.put(s.getSystemId() + "|" + s.getResourceCode(), s);
		}

		SystemBusiStepRelationConf systemBusiStepRelationConf = new SystemBusiStepRelationConf();
		systemBusiStepRelationConf.setSysBusiProcessConfigMap(map);
		return systemBusiStepRelationConf;
	}

	@Override
	public WebFilterConf getWebFilterConf() {
		Map<String, Integer> map = new HashMap<>();
		List<String> list = flowConfMapper.queryWebFilterList();
		list.forEach(k -> map.put(k, 1));

		WebFilterConf webFilterConf = new WebFilterConf();
		webFilterConf.setFilterMap(map);
		return webFilterConf;
	}

	@Override
	public WebStepIpSegmentComRelationConf getWebStepIpSegmentComRelationConf() {
		Map<String, Map<String, List<SegmentComponentRelation>>> map = new HashMap<>();
		Map<String, List<SegmentComponentRelation>> stepMap = getIpSegmentComRelationConf(Collections.singletonList("STEP")).getIpSegComRelationshipConfig();
		Map<String, List<SegmentComponentRelation>> webMap = getIpSegmentComRelationConf(Collections.singletonList("WEB")).getIpSegComRelationshipConfig();
		map.put("STEP", stepMap);
		map.put("WEB", webMap);

		WebStepIpSegmentComRelationConf webStepIpSegmentComRelationConf = new WebStepIpSegmentComRelationConf();
		webStepIpSegmentComRelationConf.setSegmentComponentRelationshipConfig(map);
		return webStepIpSegmentComRelationConf;
	}

	@Override
	public WhiteListConf getWhiteListConf() {
		Map<Integer, List<String>> whiteListConfig = new HashMap<>();
		List<ConfResourceWhitelist> list = flowConfMapper.queryWhiteList();
		for (ConfResourceWhitelist c : list) {
			List<String> urlList = whiteListConfig.computeIfAbsent(c.getType(), f -> new ArrayList<>());
			urlList.add(c.getUrl());
		}

		// 加载包含类型为STEP和LOAD的白名单配置
		List<String> loadAndStepWhiteList = new ArrayList<>();
		List<String> stepWhiteList = whiteListConfig.get(0);
		List<String> loadWhiteList = whiteListConfig.get(1);
		loadAndStepWhiteList = Stream.of(stepWhiteList, loadWhiteList)
				.filter(Objects::nonNull)
				.flatMap(Collection::stream)
				.collect(Collectors.toList());

		WhiteListInfo whiteListInfo = new WhiteListInfo();
		whiteListInfo.setWhiteListConfig(whiteListConfig);
		whiteListInfo.setLoadAndStepWhiteList(loadAndStepWhiteList);

		WhiteListConf whiteListConf = new WhiteListConf();
		whiteListConf.setWhiteListInfo(whiteListInfo);

		return whiteListConf;
	}

	@Override
	public SegmentResourceExistConf getSegmentResourceExistConf() {
		Map<String, Boolean> map = new HashMap<>();
		List<String> segResources = flowConfMapper.querySegmentResourceExistList();
		segResources.forEach(s -> map.put(s, Boolean.TRUE));

		SegmentResourceExistConf segmentResourceExistConf = new SegmentResourceExistConf();
		segmentResourceExistConf.setSegmentResourceExistConfig(map);
		return segmentResourceExistConf;
	}

	@Override
	public FlowParamsConf getFlowParamsConf() {
		Map<String, String> flowParms = new HashMap<>();
		List<SysDictItem> flow = remoteDictService.getDictByType("F").getData();
		flow.forEach(item -> flowParms.put(item.getLabel(), item.getItemValue()));

		FlowParamsConf flowParamsConf = new FlowParamsConf();
		flowParamsConf.setFlowParamsConfig(flowParms);
		return flowParamsConf;
	}

	@Override
	public OriDataFilterConf getOriDataFilterConf() {
		List<OriginalDataFilterEntity> originalDataFilterEntityList = flowConfMapper.getOriDataFilterList();
		List<FilterEntity> filterRule = getFilterRule(originalDataFilterEntityList);

		OriDataFilterConf oriDataFilterConf = new OriDataFilterConf();
		oriDataFilterConf.setFilterRule(filterRule);

		return oriDataFilterConf;
	}

	/**
	 * 获取封装的过滤规则
	 */
	public List<FilterEntity> getFilterRule(List<OriginalDataFilterEntity> filterEoList) {
		Splitter SPLITTER_COMMA = Splitter.on(",");
		Splitter SPLITTER_UNION_SEMICOLON = Splitter.on("\\:");
		Splitter SPLITTER_UNION_COMMA = Splitter.on("\\,");

		List<FilterEntity> result = new ArrayList<>();
		for (OriginalDataFilterEntity originalDataFilterEo : filterEoList) {
			FilterEntity filter = new FilterEntity();
			Integer id = originalDataFilterEo.getId();
			String resourceCoding = originalDataFilterEo.getResourceCoding();

			// 目前逻辑中需要的是时间戳
			Date startTime = originalDataFilterEo.getStartTime();
			Date endTime = originalDataFilterEo.getEndTime();
			Integer captureCount = originalDataFilterEo.getCaptureCount();
			String requestIp = originalDataFilterEo.getRequestIp();
			String responseIp = originalDataFilterEo.getResponseIp();
			String keyword = originalDataFilterEo.getKeyword();

			// 开始封装对象
			filter.setId(id);
			filter.setIsStatus(originalDataFilterEo.getIsStatus());
			filter.setStatus(originalDataFilterEo.getStatus());
			filter.setDr(originalDataFilterEo.getDr());
			filter.setEnable(originalDataFilterEo.getEnable());

			// 资源编码
			if (StringUtils.isNotBlank(resourceCoding)) {
				Iterable<String> split = SPLITTER_COMMA.omitEmptyStrings().split(resourceCoding);
				Map<String, Long> resourceCodings = filter.getResourceCodings();
				split.forEach(obj -> {
					resourceCodings.put(obj, 0L);
				});
				filter.setFilterByResouceCoding(true);
			}

			// 请求ip  in\:127.0.0.1,转换为对应的 long
			if (StringUtils.isNotBlank(requestIp)) {
				List<String> split = SPLITTER_UNION_SEMICOLON.omitEmptyStrings().splitToList(requestIp);
				filter.setRequestIpRule(split.get(0));
				Iterable<String> iterable = SPLITTER_UNION_COMMA.omitEmptyStrings().split(split.get(1));
				Iterator<String> iterator = iterable.iterator();
				StringBuilder requestIpStr = new StringBuilder();
				while (iterator.hasNext()) {
					String ip2Long = IpUtil.customIpStr2number(iterator.next());
					requestIpStr.append(ip2Long).append(",");
				}
				filter.setRequestIps(requestIpStr.substring(0, requestIpStr.length() - 1));
				filter.setFilterByReqIp(true);
			}

			// 响应ip prep\:10.1.2.127\,12.13.15.168
			if (StringUtils.isNotBlank(responseIp)) {
				List<String> split = SPLITTER_UNION_SEMICOLON.omitEmptyStrings().splitToList(responseIp);
				filter.setResponseIpRule(split.get(0));
				Iterable<String> iterable = SPLITTER_UNION_COMMA.omitEmptyStrings().split(split.get(1));
				Iterator<String> iterator = iterable.iterator();
				StringBuilder responseIpStr = new StringBuilder();
				while (iterator.hasNext()) {
					String ip2Long = IpUtil.customIpStr2number(iterator.next());
					responseIpStr.append(ip2Long).append(",");
				}
				filter.setResponseIps(responseIpStr.substring(0, responseIpStr.length() - 1));
				filter.setFilterByResIp(true);
			}

			// 关键字
			if (StringUtils.isNotBlank(keyword)) {
				Iterable<String> keyWordIterable = SPLITTER_UNION_COMMA.omitEmptyStrings().split(keyword);
				Map<String, String> keyWorlds = filter.getKeyWorlds();
				keyWordIterable.forEach(obj -> {
					List<String> semicolonList = SPLITTER_UNION_SEMICOLON.splitToList(obj);
					keyWorlds.put(semicolonList.get(1), semicolonList.get(0));
				});
				filter.setFilterByKeyword(true);
			}

			// 过滤开始时间结束时间
			if (null != startTime && null != endTime) {
				filter.setStartTime(startTime.getTime() * 1000);
				filter.setEndTime(endTime.getTime() * 1000);
				filter.setFilterByTime(true);
			}

			if (captureCount != null) {
				filter.setCaptureCount(Long.parseLong(captureCount + ""));
			}

			result.add(filter);
		}
		return result;
	}

	@Override
	public NetworkAgentConf getNetworkAgentMap() {
		Map<String, ModuleNetworkAgentVo> networkMap = new HashMap<>();
		List<ModuleNetworkAgentVo> voList = flowConfMapper.queryNetworkAgentList();
		voList.forEach(item -> networkMap.put(item.getAgentIp(), item));
		NetworkAgentConf agentConf = new NetworkAgentConf();
		agentConf.setNetworkAgentConf(networkMap);
		return agentConf;
	}

}
